﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.Domain.Shared.ValidateAttribute
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
    public class ValuesValidateAttribute : ValidationAttribute
    {
        public string[]? StrValues = null;
        public int[]? IntValues = null;

        /*public ValuesValidateAttribute(string[] strValues) {
            this.StrValues = strValues;
        }

        public ValuesValidateAttribute(int[] intValues) {
            this.IntValues = intValues;
        }*/

        public override bool IsValid(object value)
        {
            if (value == null)
            {
                this.ErrorMessage = "请填写数据";
                return false;
            }
            if (StrValues != null) {
                if (!StrValues.Contains(value.ToString())) {
                    return false;
                }
                return true;
            } else if (IntValues != null) {
                if (!IntValues.Contains(int.Parse(value.ToString()))) {
                    return false;
                }
                return true;
            }
            this.ErrorMessage = "数据有误";
            return false;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
            {
                return new ValidationResult("请填写数据");
            }
            if (StrValues != null)
            {
                if (!StrValues.Contains(value.ToString()))
                {
                    return new ValidationResult("数据有误");
                }
                return ValidationResult.Success;
            }
            else if (IntValues != null)
            {
                if (!IntValues.Contains(int.Parse(value.ToString())))
                {
                    return new ValidationResult("数据有误");
                }
                return ValidationResult.Success;
            }
            return new ValidationResult("数据有误");
        }

    }
}
